package com.atguigu.gulicollege.service_gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author sunday
 * @date 2020-07-24 16:09
 */
@Configuration
public class GulicollegeCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter(){

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        // 跨域配置
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");    // 允许跨域访问的请求头
        corsConfiguration.addAllowedMethod("*");    // 允许跨域的方法类型
        corsConfiguration.addAllowedOrigin("*");    // 允许跨域任意请求来源
        corsConfiguration.setAllowCredentials(true);    // 允许跨域请求携带cookie

        //    “/**” 任意请求路径都需要跨域配置
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);

    }

}
